package com.iflytek.edu.db.agent.utils;

import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import javassist.bytecode.AccessFlag;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;

/**
 * @author linkai
 * @date 2022/2/18
 */
public class PrintUtils {
    public static void printMethodInfo(CtClass ctClass, CtMethod ctMethod) throws NotFoundException {
        final MethodInfo methodInfo = ctMethod.getMethodInfo();
        boolean isStatic = (methodInfo.getAccessFlags() & AccessFlag.STATIC) != 0;
        String clazzName = ctClass.getName();
        String methodName = ctMethod.getName();
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
        CtClass[] parameterTypes = ctMethod.getParameterTypes();

        CtClass returnType = ctMethod.getReturnType();
        String returnTypeName = returnType.getName();

        System.out.println("=====> 类名：" + clazzName);
        System.out.println("=====> 方法：" + methodName);
        System.out.println("=====> 类型：" + (isStatic ? "静态方法" : "非静态方法"));
        System.out.println("=====> 描述：" + methodInfo.getDescriptor());
        System.out.println("=====> 入参[名称]：" + attr.variableName(1) + "，" + attr.variableName(2));
        System.out.println("=====> 入参[类型]：" + parameterTypes[0].getName() + "，" + parameterTypes[1].getName());
        System.out.println("=====> 出参[类型]：" + returnTypeName);
    }

}
